const {getList,getDetail,newBlog,updateBlog,delBlog}=require('../controller/blog')
const {SuccessModel,ErrorModel}=require('../model/resModel')

const handleBlogRouter =(req,res)=>{
    const method=req.method
    const id=req.query.id
   
    if(method==='GET'&&req.path==='/api/blog/list'){
        const author =req.query.author || ''
        const keyword=req.query.keyword || ''
        const result =getList(author,keyword)
       return result.then(listData=>{
            return new SuccessModel(listData)
        })
        
    }
    //获取博客详情
    if(method==='GET'&&req.path==='/api/blog/detail'){
        
        // const data=getDetail(id)
        // return new SuccessModel(data)        
        const result= getDetail(id)
        return result.then(data=>{
            return new SuccessModel(data)
        })

    }
    //新建博客
    if(method==='POST'&&req.path==='/api/blog/new'){
       const author='zhanghao'
       req.body.author=author
       const result=newBlog(req.body)
       return result.then(data=>{
           return new SuccessModel(data)
       })
    }
    //更新博客
    if(method==='POST'&&req.path==='/api/blog/update'){
        const result=updateBlog(id,req.body)
       return result.then(val=>{
            if(val){
                return new SuccessModel()
            }else{
                return new ErrorModel("更新博客失败")
            }
        })
        
    }
    //删除博客
    if(method==='POST'&&req.path==='/api/blog/del'){
        const author='zhanghao'
        const del=delBlog(id,author)
       return del.then(val=>{
            if(val){
                return new SuccessModel("删除成功")
            }
            else{
                return new ErrorModel('删除失败')
            }
        })
        
    }
}
module.exports=handleBlogRouter